﻿@{
    ViewBag.Title = "ShowNotify";
    Layout = null;
    var lst = ViewData["lst"] as List<SmallJob.Model.MMessage>;
}
<div class="popover bottom" style="top: 40px; display: none; position: fixed; right: 150px; left: auto; width: 350px; max-width: 350px;" id="notify-list-details">
    <div class="arrow"></div>
    <h3 class="popover-title">Nhắc nhở của bạn</h3>
    <div class="popover-content">
        <div class="conversation-detail-inner" style="max-height: 300px; overflow-y: scroll;" id="notify-list-details-content">
            @if (lst.Count <= 0)
            {
                <li>Không có nhắc nhở nào cho bạn
                </li>
            }
            @foreach (var item in lst)
            {
                var sender = (item.SenderID == (int)SmallJob.Model.EUserID.SystemUser ? null : (item.SenderID == item.Parent.Person1ID ? item.Parent.User1 : item.Parent.User2));
                <div class="conversation-message notify-item" onclick="viewnotifydetails(this,'@item.Id',event);" id="notify-parent-@item.ParentID">
                    <div class="conversation-avatar pull-left">
                        <img style="width:32px;height:32px;" src="@(sender == null ? "https://fbcdn-profile-a.akamaihd.net/hprofile-ak-prn2/276736_493733973997031_1129115008_q.jpg" : (string.IsNullOrWhiteSpace(sender.Avatar) ? Url.Content("~/Content/img/blank-avatar.jpg") : sender.Avatar) + "?w=32&h=32&q=90&format=jpg&mode=crop")" />
                    </div>
                    <div class="conversation-body">
                        <p class="inbox-meta">
                            <a   class="inbox-sender" href="/@(sender == null ? "Home/Index" : (string.IsNullOrWhiteSpace(sender.UserName) ? sender.ID.ToString() : sender.UserName))">@(sender == null ? "Việc Nhỏ" : sender.Name)  </a>
                            : @item.Content

                        </p>
                        <p>
                            @if (!item.IsRead)
                            {
                                <span class="pull-left label label-important" data-notifyid="@item.Id">New</span>
                            }
                            <span class="inbox-date pull-right">@item.Date.ToString("dd/MM/yyyy hh:mm tt")</span>
                        </p>
                    </div>
                    <div style="clear: both;"></div>
                </div>
            }
        </div>
    </div>
</div>
<a href="/Message/ShowNotifyPV?page=@(ViewData["nextpage"] ?? 0)" class="infinite-more-link"></a>
<script>
    //$('#notify-list-details-content').waypoint('infinite', {
    //    container: 'auto',
    //    items: '.conversation-message',
    //    more: '.infinite-more-link',
    //    offset: 'bottom-in-view',
    //    loadingClass: 'infinite-loading',
    //    onBeforePageLoad: $.noop,
    //    onAfterPageLoad: $.noop
    //});
    var stop = false;
    $('#notify-list-details-content').scroll(function (data) {
        if ($(this).scrollTop() == ($('#notify-list-details-content')[0].scrollHeight - $(this).height()) && !stop) {
            stop = true;
            var remember = $('a.infinite-more-link');
            var link = $('a.infinite-more-link').attr('href');
            $.post(link, {}, function (data) {
                var html = $(data).find('.notify-item');
                $('#notify-list-details-content').append(html);
                setListenerReadNotify();
                var more = $(data).filter('a.infinite-more-link');
                $(remember).replaceWith(more);
                stop = false;
            });
        }

    });
    function viewnotifydetails(that,notifyid, e) {
        //if ($(e.target).is('a')) {
        //    return;
        //} else {
            var linkto = $(that).attr('id').substring('notify-parent-'.length);
            window.location = "/Message/RedirectNotify?to=" + linkto + "&notifyid=" + notifyid;
        //}
    }

    function setreadnotify(that) {
        var id = $(that).find('span[data-notifyid]').attr('data-notifyid');
        $.post("/Message/SetReadNotify", { notifyid: id }, function (data) {
            if (data == true) {
                $(that).find('span[data-notifyid]').remove();
                loadcountnotify();
            }
        });
    }
    function setListenerReadNotify() {
        $('.notify-item').hover(function () {
            if ($(this).find('span[data-notifyid]').length !== 0) {
                setreadnotify(this);
            }
        });
    }
    $(document).ready(function () {
        setListenerReadNotify();
    });
</script>
